<?php

/*
 * This validates admin and users.
 */

class Validations_User {

    function __construct() {
        
    }

    public function getUserLayout() {
        $userId = $this->getUserTypeId();
        if ($userId == 0) {
            return "admin";
        } else if ($userId == 1) {
            return "user";
        } else {
            return "layout";
        }
    }

    public function validateUser($userType) {
        if ($this->hasidentity()) {
            $data = $this->getInstanceData();
            $userTypeId = $data->user_type;
            switch ($userType) {
                case 'admin':
                    if ($userTypeId != 1) {
                        $this->clearInstance();
                        return false;
                    }
                    break;
                case 'user':
                    if ($userTypeId != 2) {
                        $this->clearInstance();
                    }
                    break;
                default:
                    break;
            }
        } else {
            return false;
        }
        return true;
    }

    public function hasidentity() {
        if (Zend_Auth::getInstance()->hasIdentity()) {
            return true;
        } else {
            return false;
        }
    }

    public function getInstanceData() {
        $auth = Zend_Auth::getInstance();
        return $auth->getIdentity();
    }

    public function getUserTypeId() {
        $data = $this->getInstanceData();
        return $data->user_type;
    }

    public function getUserId() {
        $data = $this->getInstanceData();
        return $data->id;
    }

    public function getUserName() {
        $data = $this->getInstanceData();
        $name = $data->name;
        if ($name == null) {
            $name = $data->email;
        }
        return $name;
    }

    public function clearInstance() {
        Zend_Auth::getInstance()->clearIdentity();
    }

}
